package com.useeinfo.oa.modules.security.biz;

import com.useeinfo.framework.extend.biz.CrudBiz;
import com.useeinfo.framework.sugar.data.QueryParam;
import com.useeinfo.framework.sugar.tools.AnalysisObjectList;
import com.useeinfo.oa.modules.base.vo.MessageVO;
import com.useeinfo.oa.modules.security.dao.UserDeviceDao;
import com.useeinfo.oa.modules.security.entity.SystemUser;
import com.useeinfo.oa.modules.security.entity.UserDevice;
import com.useeinfo.oa.modules.wuHanCommon.base.entity.WarningGroup;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service("UserDeviceBiz")
public class UserDeviceBiz extends CrudBiz<UserDeviceDao, UserDevice> {

    public List<MessageVO> getUserDeviceListByWarningGroupId(WarningGroup warningGroup) {
        if (warningGroup == null) {
            return null;
        }
        List<MessageVO> messageVOS = new ArrayList<>();
        List objects = dao.getUserDeviceListByWarningGroupId(warningGroup.getId());
        if (CollectionUtils.isNotEmpty(objects)) {
            for (Object object : objects) {
                net.sf.json.JSONArray j = net.sf.json.JSONArray.fromObject(object);
                messageVOS.add(new MessageVO(AnalysisObjectList.getJsonString(j, 0), AnalysisObjectList.getJsonString(j, 1)));
            }
        }
        return messageVOS;
    }


    public SystemUser getUserMessage(String clientid) {
        if (StringUtils.isBlank(clientid)) {
            return null;
        }
        QueryParam queryParam = new QueryParam();
        queryParam.getSqlMap().put("deviceToken", clientid);
        List<UserDevice> userDevices = this.findList(queryParam);
        if (CollectionUtils.isEmpty(userDevices)) {
            return null;
        }
        return userDevices.get(0).getSystemUser();
    }

}
